﻿package njClass.bat{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	//import gs.TweenLite;
	
	public class MovePlus {
		//随机位移
		public static function ramdonMove(Obj:DisplayObject):void {
			var speed:int=Math.floor(Math.random() * 4 + 1);
			var degrees:int=Math.floor(Math.random() * 360);
			Obj.addEventListener(Event.ENTER_FRAME,moveAct);
			
			function moveAct(e:Event):void {
				if (Obj.alpha <= 0) {
					Obj.removeEventListener(Event.ENTER_FRAME,moveAct);
					return;
				}
				degrees=degrees + Math.floor(Math.random() * 60 - 30);
				var radians:Number=degrees * Math.PI / 180;
				Obj.x+= speed * Math.cos(radians);
				Obj.y+= speed * Math.sin(radians);
			}
		}
		
		//拖动移动
		public static function SliderMove(Obj:Sprite,bg:Sprite,distant:Boolean=true):void {
			Obj.addEventListener(MouseEvent.MOUSE_DOWN,ObjMove);
			
			function ObjMove(e:MouseEvent):void {
				var rect:Rectangle;
				if (distant) {
					rect=new Rectangle(bg.x,bg.y,bg.width - Obj.width,0);
				} else {
					rect=new Rectangle(bg.x,bg.y,0,bg.height - Obj.height);
				}
				Obj.startDrag(false,rect);
				Obj.stage.addEventListener(MouseEvent.MOUSE_UP,ObjStand);
			}
			
			function ObjStand(e:MouseEvent):void {
				Obj.stopDrag();
				Obj.stage.removeEventListener(MouseEvent.MOUSE_UP,ObjStand);
			}
		}
		
		//跟随鼠标
		public static function followMove(Obj:*,bg:DisplayObject,number:Number=1,distance:Number=20):void {
			var balls:Array=new Array;
			
			for (var i:int=0; i < number; i++) {
				var ball:*=new Obj;
				BallMove(ball,i,balls,bg.stage,distance);
				balls.push(ball);
				ball.scaleX=1 - i * 0.05;
				ball.scaleY=1 - i * 0.05;
				ball.alpha=1 - i * 0.1;
				bg.stage.addChild(ball);
			}
		}
		
		private static function BallMove(Obj:DisplayObject,id:Number,balls:Array,bg:DisplayObject,distance:Number=20):void {
			Obj.addEventListener(Event.ENTER_FRAME,withMouse);
			
			function withMouse(e:Event):void {
				var point1:Point;
				if (id == 0) {
					point1=new Point(bg.mouseX,bg.mouseY);
				} else {
					point1=new Point(balls[id - 1].x,balls[id - 1].y);
				}
				var point2:Point=new Point(Obj.x,Obj.y);
				if (Point.distance(point1,point2) < distance) {
					return;
				}
				Obj.x+= (point1.x - point2.x) / 5;
				Obj.y+= (point1.y - point2.y) / 5;
			}
		}
		
		//变速移动
		public static function ChangeSpeedMove(Obj:DisplayObject,targetX:Number,targetY:Number,time:int=3):void {
			//TweenLite.to(Obj,time,{"x":targetX,"y":targetY});
		}
	}
}